<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="本地路径" prop="localPath">
        <el-input
            v-model="queryParams.localPath"
            placeholder="请输入本地存放的路径"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版本号" prop="flinkVersion" label-width="20">
        <el-input
            v-model="queryParams.flinkVersion"
            placeholder="请输入版本号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['resource:flinkversions:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList(false)"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flinkversionsList" @selection-change="handleSelectionChange"  border  highlight-current-row align="center">
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="本地存放的路径" align="center" prop="localPath"/>
      <el-table-column label="版本号" align="center" prop="flinkVersion"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
      <el-table-column prop="inUse" label="是否使用中" align="center" min-width="200">
        <template slot-scope="scope">
          <TableState v-if="scope.row.inUse" showTitle="使用中" stateType="positive"/>
          <TableState v-else showTitle="未使用" stateType=""/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['resource:flinkversions:edit']"
              v-if="!scope.row.inUse"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['resource:flinkversions:remove']"
              v-if="!scope.row.inUse">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList(false)"
    />

    <!-- 添加或修改Flink版本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="路径" prop="localPath">
          <el-input v-model="form.localPath" placeholder="请输入本地存放的路径"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"
                   :loading="isSave"
                   @click="submitForm">

          <span v-if="!isSave">保存</span>
          <span v-else>正在保存...</span>

        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFlinkversions,
  getFlinkversions,
  delFlinkversions,
  addFlinkversions,
  updateFlinkversions
} from "@/api/resource/flinkversions";
import TableState from "@/components/TableState";

export default {
  name: "Flinkversions",
  components: {
    TableState
  },
  data() {
    return {
      // 遮罩层
      isSave: false,
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Flink版本信息表格数据
      flinkversionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        localPath: null,
        flinkVersion: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        localPath: [
          {required: true, message: "本地存放的路径不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList(false);
  },
  mounted() {
    const timer = window.setInterval(() => this.getList(true), 2000)
    this.$once('hook:beforeDestroy', () => {
      clearInterval(timer)
    })
  },
  methods: {

    /** 查询Flink版本信息列表 */
    getList(isTimer) {
      if(!isTimer)
      {
        this.loading = true;
      }
      listFlinkversions(this.queryParams).then(response => {
        this.flinkversionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        localPath: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(false);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加Flink版本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFlinkversions(id).then(response => {
        if (response.data.inUse) {
          this.$message.warning('正在使用的版本不能修改！')
          return;
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改Flink版本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.isSave = true;
          if (this.form.id != null) {
            updateFlinkversions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList(false);
              this.isSave = false;
            }).catch(err => {
              this.$message.error(err)
              this.isSave = false;
            });
          } else {
            addFlinkversions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList(false);
              this.isSave = false;
            }).catch(err => {
              this.$message.error(err)
              this.isSave = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除Flink版本信息编号为"' + ids + '"的数据项？').then(function () {
        return delFlinkversions(ids);
      }).then(() => {
        this.getList(false);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/flinkversions/export', {
        ...this.queryParams
      }, `flinkversions_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
